package com.itheima.Controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
    public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        log.info("文件上传开始：{}", file.getOriginalFilename());
        //文件交给阿里云服务器管理
        try {
            String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
            log.info("文件上传成功：{}", url);
            return Result.success(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败");
        }

    }










    /*   *//**
     * 本地磁盘存储的方法
     * @param username
     * @param age
     * @param file
     * @return
     * @throws IOException
     *//*
    @PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile file) throws IOException {

        String originalFilename = file.getOriginalFilename();// 获取上传的文件的原始文件名
        // 获取上传的文件的原始文件名的后缀名
        String EndName = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 拼接新的文件名
        String NewFileName = UUID.randomUUID().toString() + EndName;
        //保存指定路径的文件
        file.transferTo(new File("D:/images/" + NewFileName));
        return Result.success();
    }*/
}
